package com.liu.nb.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.liu.nb.dto.DebugQueryDto;
import com.liu.nb.entity.Receiptpatrol;
import com.liu.nb.util.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 巡逻回执任务表 前端控制器
 * </p>
 *
 * @author yzl
 * @since 2021-08-31
 */
@RestController
@RequestMapping("/nb/receiptpatrol")
public class ReceiptpatrolController extends BaseController {

    /**
     * 巡检员回执录入
     *
     * @param receiptpatrol
     * @return
     */
    @PreAuthorize("hasRole('xuanjianyuan')")
    @PostMapping("/savaReceipt")
    public Result savaReceipt(@RequestBody Receiptpatrol receiptpatrol, Principal principal) {
        receiptpatrol.setUid((iUserService.getByUsername(principal.getName())).getId());
        Integer count = receiptpatrolService.savaReceipt(receiptpatrol);
        if (count > 0) {
            return Result.success(true);
        } else {
            return Result.success(false);
        }

    }

    /**
     * 巡检员修改回执信息回填
     * @param ptid
     * @param pid
     * @return
     */
    @PreAuthorize("hasRole('xuanjianyuan')")
    @PostMapping("/getReceipt/{ptid}/{pid}")
    public Result getReceipt(@PathVariable String ptid, @PathVariable String pid) {
        Receiptpatrol receipt = receiptpatrolService.getReceipt(ptid, ptid);
        return Result.success(receipt);
    }


    /**
     * 巡检员修改回执录入
     *
     * @param receiptpatrol
     * @param principal
     * @return
     */
    @PreAuthorize("hasRole('xuanjianyuan')")
    @PostMapping("/updateReceipt")
    public Result updateReceipt(@RequestBody Receiptpatrol receiptpatrol, Principal principal) {
        receiptpatrol.setUid((iUserService.getByUsername(principal.getName())).getId());
        Integer count = receiptpatrolService.updateReceipt(receiptpatrol);
        if (count > 0) {
            return Result.success(true);
        } else {
            return Result.success(false);
        }
    }


    /**
     *巡检员和线路管理员，缺陷查询
     * @param receiptpatrol
     * @param cid
     * @param current
     * @param size
     * @return
     */
    @PreAuthorize("hasAnyRole('xianluadmin','xuanjianyuan')")
    @PostMapping ("/getReceipt")
    public Result getReceipt(Receiptpatrol receiptpatrol, String cid, Integer current, Integer size){
        IPage<Receiptpatrol> receiptAll = receiptpatrolService.getReceiptAll(receiptpatrol, cid, current, size);
        return Result.success(receiptAll);
    }

    //查询未设置缺陷级别的数据
    @PreAuthorize("hasAnyRole('xianluadmin','root')")
    @PostMapping("/getBylevel/{pageNo}/{pageSize}")
    public Result getBylevel(@RequestBody Receiptpatrol receiptpatrol,@PathVariable Integer pageNo,@PathVariable Integer pageSize){
        return Result.success(receiptpatrolService.getBylevel(receiptpatrol,pageNo,pageSize));
    }
    //更新缺陷级别
    @PreAuthorize("hasAnyRole('xianluadmin','root')")
    @PostMapping("/updlevel/{level}/{id}")
    public Result updLevel(@PathVariable Integer level,@PathVariable Integer id){
        Receiptpatrol receiptpatrol=new Receiptpatrol();
        receiptpatrol.setId(id);
        receiptpatrol.setLevel(level);
        return Result.success(receiptpatrolService.updateById(receiptpatrol));
    }

    @PostMapping("/getDebugQuery/{current}/{size}")
    public Result getDebugQuery(@RequestBody HashMap<String,String> map,@PathVariable int current,@PathVariable int size){
        IPage<DebugQueryDto> debugQuery = receiptpatrolService.getDebugQuery(current, size, map);
        return Result.success(debugQuery);
    }
}
